package com.framework.loippi.controller.admin;

import com.framework.loippi.controller.GenericController;
import com.framework.loippi.entity.ParameterSetting;
import com.framework.loippi.service.ParameterSettingService;
import com.framework.loippi.support.Message;
import com.framework.loippi.support.Pageable;
import com.framework.loippi.utils.JsonUtils;
import com.framework.loippi.utils.ParameterUtils;
import com.framework.loippi.utils.StringUtil;
import net.sf.json.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Controller - 参数设置
 *
 * @author wmj
 * @version 2.0
 */
@Controller("adminParameterSettingController")
@RequestMapping({"/admin/parameter_setting"})
public class ParameterSettingController extends GenericController {

    @Resource
    private ParameterSettingService parameterSettingService;

    /**
     * 跳转添加页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = {"/add"}, method = {RequestMethod.GET})
    public String add(ModelMap model) {
        return "/admin/parameter_setting/add";
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(ParameterSetting parameterSetting, RedirectAttributes redirectAttributes) {
        parameterSettingService.save(parameterSetting);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.jhtml";
    }

    /**
     * 编辑
     */
    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String edit(@PathVariable Long id, ModelMap model) {
        ParameterSetting parameterSetting = parameterSettingService.find(id);
        model.addAttribute("parameterSetting", parameterSetting);
        return "/admin/parameter_setting/edit";
    }


    /**
     * 详情
     */
    @RequestMapping(value = "/view/{id}", method = RequestMethod.GET)
    public String view(@PathVariable Long id, ModelMap model) {
        ParameterSetting parameterSetting = parameterSettingService.find(id);
        model.addAttribute("parameterSetting", parameterSetting);
        return "/admin/parameter_setting/view";
    }

    /**
     * 会员体系设置
     */
    @RequestMapping(value = "/memberSystem", method = RequestMethod.GET)
    public String memberSystem(ModelMap model) {
        List<ParameterSetting> list = parameterSettingService.findAll();
        JSONArray json = JSONArray.fromObject(list);
        model.addAttribute("json", json);
        return "/admin/parameter_setting/memberSystem";
    }

    /**
     * 会员体系修改
     *
     * @param json JSON数据字符串
     * @return
     */
    @RequestMapping(value = "/update_member", method = RequestMethod.POST)
    @ResponseBody
    public Message update_member(String json) {
        if (StringUtil.isEmpty(json)) {
            return Message.error("参数异常");
        }
        List<ParameterSetting> list = (List<ParameterSetting>) JsonUtils.getList4Json(json, ParameterSetting.class);
        for (ParameterSetting setting : list) {
            parameterSettingService.update(setting);
        }
        return SUCCESS_MESSAGE;
    }

    /**
     * 总后台管理联系电话页面
     */
    @RequestMapping(value = "/phone_setting", method = RequestMethod.GET)
    public String phone_setting(ModelMap model) {
        ParameterSetting parameterSetting = parameterSettingService.find(1L);//平台联系电话
        model.addAttribute("parameterSetting", parameterSetting);
        return "/admin/parameter_setting/phone_setting";
    }


    /**
     * 更新平台手机号
     */
    @RequestMapping(value = "/update_phone", method = RequestMethod.POST)
    @ResponseBody
    public Message update_phone(ParameterSetting parameterSetting) {
        parameterSettingService.update(parameterSetting);
        return SUCCESS_MESSAGE;
    }

    /**
     * 更新
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(ParameterSetting parameterSetting, RedirectAttributes redirectAttributes) {
        parameterSettingService.update(parameterSetting);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.jhtml";
    }

    /**
     * 列表查询
     *
     * @param pageable
     * @param model
     * @return
     */
    @RequestMapping(value = {"/list"}, method = {RequestMethod.GET})
    public String list(Pageable pageable, HttpServletRequest request, ModelMap model) {

        Map<String, Object> paramter = ParameterUtils.getParametersMapStartingWith(request, "filter_");
        Map map = new HashMap();
        for (String key : paramter.keySet()) {
            if (!StringUtil.isEmpty(paramter.get(key).toString())) {
                map.put(key, paramter.get(key));
            }
        }
        pageable.setParameter(map);
        model.addAttribute("paramter", paramter);
        model.addAttribute("page", this.parameterSettingService.findByPage(pageable));
        return "/admin/parameter_setting/list";
    }

    /**
     * 删除操作
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = {"/delete"}, method = {RequestMethod.POST})
    public @ResponseBody
    Message delete(Long[] ids) {
        this.parameterSettingService.deleteAll(ids);
        return SUCCESS_MESSAGE;
    }
}
